using UnityEngine;
using UnityEngine.Serialization;
#if UNITY_EDITOR
using UnityEditor;
#endif


namespace HEFramework
{
    public enum eDomain
    {
        Unity,
        Resources,
        Streaming,
        Persistent,
    }

    /// <summary>
    ///
    /// AB资源设置数据体
    /// 
    /// ----------------------------------------
    /// 作者 hep
    /// 创建时间：2023-12-21 17:31:56
    /// ----------------------------------------
    /// </summary>
    public class AssetBundleSettingScriptableObject : ScriptableObject
    {
        private static AssetBundleSettingScriptableObject instance;

        public static AssetBundleSettingScriptableObject Instance
        {
            get
            {
                if (instance != null)
                {
                    return instance;
                }

                instance = Resources.Load<AssetBundleSettingScriptableObject>(ASSETBUNDLE_SETTING_SAVE_FILE_NAME);
                if (instance != null)
                {
                    return instance;
                }

                instance = CreateInstance<AssetBundleSettingScriptableObject>();

                instance.ResetAll();

#if UNITY_EDITOR

                if (!System.IO.Directory.Exists(ASSETBUNDLE_SETTING_SAVE_PATH))
                {
                    System.IO.Directory.CreateDirectory(ASSETBUNDLE_SETTING_SAVE_PATH);
                    UnityEditor.AssetDatabase.Refresh();
                }

                UnityEditor.AssetDatabase.CreateAsset(instance, ASSETBUNDLE_SETTING_SAVE_FILE_PATH);
#endif
                return instance;
            }
        }


        /// <summary>
        /// 数据体存储名字
        /// </summary>
        private const string ASSETBUNDLE_SETTING_SAVE_FILE_NAME = "AssetBundleSettings";

        /// <summary>
        /// 数据体文件后缀
        /// </summary>
        private const string ASSETBUNDLE_SETTING_FILE_EXTENSION = ".asset";

        /// <summary>
        /// 数据体保存路径
        /// </summary>
        private const string ASSETBUNDLE_SETTING_SAVE_PATH = "Assets/Resources";

        /// <summary>
        /// 数据体文件保存路径
        /// </summary>
        private const string ASSETBUNDLE_SETTING_SAVE_FILE_PATH = "Assets/Resources/" + ASSETBUNDLE_SETTING_SAVE_FILE_NAME + ASSETBUNDLE_SETTING_FILE_EXTENSION;


        /// <summary>
        /// AssetBundle表格清单文件名称
        /// </summary>
        private const string ASSETBUNDLE_EXCEL_FILE_NAME = "csvInfos.csv";

        /// <summary>
        /// AssetBundle验证文件名称
        /// </summary>
        private const string ASSETBUNDLE_CHECK_FILE_NAME = "file.check";

        /// <summary>
        /// AssetBundle中心文件名称
        /// </summary>
        private const string ASSETBUNDLE_CENTRAL_FILE_NAME = "bundle";


        /// <summary>
        /// AssetBundle数据原始路径
        /// </summary>
        [SerializeField] private string assetBundleDataOriginalPath = "Assets/AssetBundles";

        /// <summary>
        /// AssetBundle数据输出路径
        /// </summary>
        [SerializeField] private string assetBundleDataBuildTargetPath = "Assets/../AssetBundles";

        /// <summary>
        /// 环境
        /// </summary>
        [SerializeField] private eDomain domain = eDomain.Unity;

        /// <summary>
        /// AssetBundle环境目录
        /// </summary>
        [SerializeField] private string assetBundleDataDomainFolder = "AssetBundles";


        /// <summary>
        /// AssetBundle数据下载URL
        /// </summary>
        [SerializeField] private string assetBundleDataDownloadURL = "";

        /// <summary>
        /// 加密
        /// </summary>
        [SerializeField] private bool encrypt = false;

        /// <summary>
        /// 加密钥匙
        /// </summary>
        [SerializeField] private string key = "qUai93hxtqhUFhF6";

        /// <summary>
        /// 加密Iv
        /// </summary>
        [SerializeField] private string iv = "kpDvS3PtFjXCv3iN";

#if UNITY_EDITOR
        /// <summary>
        /// 压缩模式
        /// </summary>
        [SerializeField] private BuildAssetBundleOptions compress = BuildAssetBundleOptions.ChunkBasedCompression;
#endif


        public string AssetbundleExcelFileName
        {
            get { return ASSETBUNDLE_EXCEL_FILE_NAME; }
        }

        public string AssetBundleCheckFileName
        {
            get { return ASSETBUNDLE_CHECK_FILE_NAME; }
        }

        public string AssetBundleCentralFileName
        {
            get { return ASSETBUNDLE_CENTRAL_FILE_NAME; }
        }

        public string AssetBundleDataOriginalPath
        {
            get { return assetBundleDataOriginalPath; }
            set
            {
                if (assetBundleDataOriginalPath == value)
                {
                    return;
                }

                assetBundleDataOriginalPath = value;
                Modified = true;
            }
        }

        public string AssetBundleDataBuildTargetPath
        {
            get { return assetBundleDataBuildTargetPath; }
            set
            {
                if (assetBundleDataBuildTargetPath == value)
                {
                    return;
                }

                assetBundleDataBuildTargetPath = value;
                Modified = true;
            }
        }

        public eDomain Domain
        {
            get { return domain; }
            set
            {
                if (domain == value)
                {
                    return;
                }

                domain = value;
                Modified = true;
            }
        }

        public string AssetBundleDataDomainFolder
        {
            get { return assetBundleDataDomainFolder; }
            set
            {
                if (assetBundleDataDomainFolder == value)
                {
                    return;
                }

                assetBundleDataDomainFolder = value;
                Modified = true;
            }
        }


        public string AssetBundleDataDownloadURL
        {
            get { return assetBundleDataDownloadURL; }
            set
            {
                if (assetBundleDataDownloadURL == value)
                {
                    return;
                }

                assetBundleDataDownloadURL = value;
                Modified = true;
            }
        }


        public bool Encrypt
        {
            get { return encrypt; }
            set
            {
                if (encrypt == value)
                {
                    return;
                }

                encrypt = value;
                Modified = true;
            }
        }

        public string Key
        {
            get { return key; }
            set
            {
                if (key == value)
                {
                    return;
                }

                key = value;
                Modified = true;
            }
        }

        public string IV
        {
            get { return iv; }
            set
            {
                if (iv == value)
                {
                    return;
                }

                iv = value;
                Modified = true;
            }
        }

#if UNITY_EDITOR
        public BuildAssetBundleOptions Compress
        {
            get { return compress; }
            set
            {
                if (compress == value)
                {
                    return;
                }

                compress = value;
                Modified = true;
            }
        }
#endif

        /// <summary>
        /// 是否储存上一次变更的属性 以便于回退操作
        /// </summary>
        private bool Modified
        {
            set
            {
#if UNITY_EDITOR
                //如果外部传值 就保存上一次数值
                if (value)
                {
                    UnityEditor.EditorUtility.SetDirty(this);
                }
#endif
            }
        }

        /// <summary>
        /// 获取AB资源路径
        /// </summary>
        /// <returns></returns>
        public string GetAssetBundleDataLoadPath()
        {
            switch (domain)
            {
                case eDomain.Unity:
                    return assetBundleDataOriginalPath;
                case eDomain.Resources:
                    return "";
                case eDomain.Streaming:
                    return (Application.streamingAssetsPath + "/" + assetBundleDataDomainFolder).RB();
                case eDomain.Persistent:
                    return (Application.persistentDataPath + "/" + assetBundleDataDomainFolder).RB();
            }

            return null;
        }

        /// <summary>
        /// 获取平台标识
        /// </summary>
        /// <returns></returns>
        public string GetPlatform()
        {
            string p = string.Empty;
#if UNITY_STANDALONE_WIN
            p = "StandaloneWindows64";
#elif UNITY_STANDALONE_OSX
            p = "StandaloneOSX";
#elif UNITY_IOS
            p = "iOS";
#elif UNITY_ANDROID
            p = "Android";
#elif UNITY_WEBGL
            p = "WebGL";
#endif
            return p;
        }

        /// <summary>
        /// 重置全部
        /// </summary>
        public void ResetAll()
        {
            assetBundleDataOriginalPath = "Assets/AssetBundles";
            assetBundleDataBuildTargetPath = "Assets/../AssetBundles";
            domain = eDomain.Unity;
            assetBundleDataDomainFolder = "AssetBundles";
            assetBundleDataDownloadURL = "";
            encrypt = false;
            key = "qUai93hxtqhUFhF6";
            iv = "kpDvS3PtFjXCv3iN";
#if UNITY_EDITOR
            compress = BuildAssetBundleOptions.ChunkBasedCompression;
#endif

            Modified = true;
        }
    }
}